var req=[
    '/static/plugins/FE/js/fe',
    '/static/plugins/FE/css/froala_editor.min.css?v=1',
    '/static/plugins/FE/css/plugins.min.css',
	'/static/plugins/codemirror/codemirror.min',
	'/static/plugins/codemirror/codemirror.min.css'
];	
define('editor',req,function(require,exports,module){
    //require.async(['froala_editor/js/plugins/image.min','froala_editor/js/plugins/file.min'],function(){
    require.async(['/static/plugins/FE/js/languages/zh_cn','/static/plugins/FE/js/t'],function(){
        $('#editor').froalaEditor({
            inlineMode: false,
            language:'zh_cn',
            heightMin:400,
            spellcheck:false,
            autosave: true,
            autosaveInterval: 180,
            saveURL: '/Article/autosave.html',
            saveParams: {
				title:function (){return $('[name=title]').val();},
				tags:function (){return $('#tags_ipt').val();},
				cid:function (){return $('[name=cid]').val();}
			},
			saveParam:'content',
            plainPaste: true,
            enableScript: false,
            heightMax:600,
            toolbarButtons:[
            "fullscreen","bold","italic","underline","strikeThrough","fontFamily","fontSize","|","color","emoticons","inlineStyle","paragraphStyle","|","paragraphFormat","align","formatOL","formatUL","insertLink","insertImage","insertVideo","insertFile","insertTable","quote","outdent","indent","insertHR",/*"undo","redo",*/"clearFormatting","insertCode","html"
            ],
            alwaysBlank: true,
            imageManagerLoadURL: "/Attach/getList.html?type=image&p=",
            imageManagerDeleteURL: "/Attach/delete.html",
            imageUploadURL: "http://up.qiniu.com",
            imageUploadToQiniu: {
                getTokenUrl: "/Attach/getToken",
                domain: "http://static.sssui.com/",
                saveUrl: "/Attach/save.html?key=",
                params: {
                    //crc32: "",
                    accept: "application/json"
                }
            },
            fileUploadURL: "http://up.qiniu.com",
            fileUploadToQiniu: {
                getTokenUrl: "/Attach/getToken",
                domain: "http://static.sssui.com/",
                saveUrl: "/Attach/save.html?key=",
                params: {
                    //crc32: "",
                    accept: "application/json"
                }
            },
        }).on('editable.afterRemoveImage', function (e, editor, $img) {
			// Set the image source to the image delete params.        
			editor.options.imageDeleteParams = {src: $img.attr('src')};
			// Make the delete request
			editor.deleteImage($img);
        });
        //$("#editor .fr-view").nanoScroller();
    });
})
define(function(require,exports,module){
	exports.init = function(){
		//$('#site-content .pure-u-2-3').removeClass('pure-u-2-3');
		//$('#editor').append($('#content').val());
	    require('editor');
	    require('tagEditor/tagEditor.min');
	    $('#tags_ipt').tagEditor({
	        initialTags: tags.split(','),
	        delimiter: ', ', /* space and comma */
	        placeholder: '请输入标签..'
	    });
	    require('dialog');
	    require('beautify');
	    $('select,:radio,:checkbox').beautify();
	    $('[name="post_type"]').change(function() {
	        var x=$('[name="post_type"]:checked').val();
	        if(x=='time'){
	            $('[name="post_time"]').show();
	        }else{
	            $('[name="post_time"]').hide();                
	        }
	    });
	   	//require('nanoscroller');
	    $('#adv_ckd_0').change(function(e){$('.adv_body').toggle();});
	    require('/static/plugins/My97DatePicker/WdatePicker.js');
	    $('[name=post_time]').focus(function(){
	        WdatePicker.call(this,{dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'%y-%M-%d %H:%i:%s'});
	    });
	    //$('[name=title]').focus(WdatePicker);
	    $('form .submit').click(function(){
	    	if($('[name=title]').val()==""){return '';}
	        var a1=$('<div id="a1" style="display:none;">'+$('#editor').froalaEditor('html.get')+'</div>');
	        a1.find('img').removeAttr('data-key').removeAttr('data-hash');
			var d = dialog({}).showModal();
	        d.content('<div class="ui-dialog-loading"><i class="fa fa-spinner"></i>&nbsp;&nbsp;Loading...</div>');
	    	SSSUI.catchImg(a1,d,function(){
				d.content('<div class="ui-dialog-loading"><i class="fa fa-spinner"></i>&nbsp;&nbsp;正在保存内容到服务器...</div>');
				var img =a1.find('img').eq(0).attr('src');
				$('#cover').val(img||'/static/cover.jpg');
				$('#content').val(a1.html());
				$.post('/Article/edit.html',$('form').serialize(),function(res){
					if(res.status==1){
						d.content(res.info);
						setTimeout(function () {
							d.close().remove();
							window.location=res.url;
						}, 2000);
					}else{       
						d.content(res.info);
						setTimeout(function (){d.close().remove();}, 1000);
					}
				},'JSON');
				
			});
	    	return false;
	    });
	    $(document).keydown(function(e) {
	        if((e.altKey && e.keyCode==83) || (e.ctrlKey && e.keyCode==13)){
	            $('form .submit').trigger('click');
	        }
    	});
	}
})